package com.carp.service.Impl;

import com.carp.common.DBResult;
import com.carp.entity.CarpUser;
import com.carp.mapper.CarpUserMapper;
import com.carp.service.CarpUserService;
import com.carp.utils.MD5Utils;
import com.carp.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class CarpUserServiceImpl implements CarpUserService {

    @Resource
    private CarpUserMapper carpUserMapper;

    //md5 盐值
    @Value("${myData.salt}")
    private String salt;

    //登录验证
    @Override
    @Transactional(isolation = Isolation.READ_UNCOMMITTED)
    public DBResult selectUser(CarpUser workUser) {
        System.out.println(workUser);
        //判断是否输入了手机号和密码
        if (workUser.getPhone().isEmpty() || workUser.getPassword().isEmpty()){
            return DBResult.error(500,"请输入手机号和密码");
        }
        //验证手机号是否符合要求
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$";
        Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(workUser.getPhone());
        if (!m.matches()){
            return DBResult.error(500,"输入的手机号不符合规则!");
        }

        //调用添加用户方法,判断用户是否是第一登录,第一次则进行注册
        Boolean aBoolean = insertUser(workUser);
        if (aBoolean){
            //登录成功,设置token
            String token = TokenUtils.getToken(workUser.getPhone(),workUser.getPassword());
            workUser.setAvatarUrl("https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg");
            workUser.setToken(token);
            workUser.setCreateTime(new Date());
            return DBResult.success(workUser);
        }


        //查询用户
        HashMap<String,Object> map = new HashMap<>();
        //添加查询条件
        map.put("phone",workUser.getPhone());

        //将前端传递的明文密码转换成md5
        String mdPS = MD5Utils.inputPassToDBPass(workUser.getPassword(),salt);

        map.put("password",mdPS);
        List<CarpUser> workUsers = carpUserMapper.selectByMap(map);

        if (workUsers.isEmpty()){
            return DBResult.error(500,"账号或密码错误!");
        }
        //登录成功,设置token
        String token = TokenUtils.getToken(workUsers.get(0).getPhone(),workUsers.get(0).getPassword());

        workUsers.get(0).setToken(token);

        return DBResult.success(workUsers.get(0));
    }

    //添加用户
    @Override
    public Boolean insertUser(CarpUser workUser) {
        HashMap<String,Object> map = new HashMap<>();
        //添加查询条件
        map.put("phone",workUser.getPhone());
        int insert = 0;
        List<CarpUser> workUsersExist = carpUserMapper.selectByMap(map);
        if (workUsersExist.isEmpty()){
            //md5进行加密
            String passMd5 = MD5Utils.inputPassToFromPass(workUser.getPassword());
            String newPass = MD5Utils.fromPassToDBPass(passMd5, salt);

            workUser.setPassword(newPass);
            insert = carpUserMapper.insert(workUser);

        }
        return insert>=1;
    }

    //修改用户
    @Override
    public Boolean updateUser(CarpUser carpUser) {
        int i = 0;
        try{
            i = carpUserMapper.updateById(carpUser);
        }catch (Exception e){

        }

        return i>=1;
    }
}
